package org.origin.centre.security.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 鉴权配置
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("ALL")
@ConfigurationProperties(prefix = "authorization")
public class AuthorizationProperty {
    /**
     * 缓存token的key
     */
    private String tokenCacheKKey = "token_key:";
    /**
     * 缓存token的秘钥key
     */
    private String tokenSecretKey = "jwk_source";
    /**
     * 自定义可以匿名方的url（或者访问白名单）
     */
    private List<String> permitAllUrls = new ArrayList<>();
    /**
     * 禁用CSRF保护
     */
    private boolean csrfDisable = true;
    /**
     * 禁用X-Frame-Options 防止iframe嵌套
     */
    private boolean frameDisable = true;

    public String getTokenCacheKKey() {
        return tokenCacheKKey;
    }

    public void setTokenCacheKKey(String tokenCacheKKey) {
        this.tokenCacheKKey = tokenCacheKKey;
    }

    public String getTokenSecretKey() {
        return tokenSecretKey;
    }

    public void setTokenSecretKey(String tokenSecretKey) {
        this.tokenSecretKey = tokenSecretKey;
    }

    public List<String> getPermitAllUrls() {
        return this.permitAllUrls;
    }

    public void setPermitAllUrls(List<String> urls) {
        if (urls != null && urls.size() > 0) {
            List<String> newUrls = new ArrayList<>();
            for (String url : urls) {
                if (url != null) {
                    String[] itemUrls = url.split(",");
                    if (itemUrls != null && itemUrls.length > 0) {
                        for (String itemUrl : itemUrls) {
                            if (StringUtils.hasText(itemUrl)) {
                                newUrls.add(itemUrl);
                            }
                        }
                    }
                }
            }
            this.permitAllUrls = newUrls;
        } else {
            this.permitAllUrls = urls;
        }
    }

    public boolean isCsrfDisable() {
        return csrfDisable;
    }

    public void setCsrfDisable(boolean csrfDisable) {
        this.csrfDisable = csrfDisable;
    }

    public boolean isFrameDisable() {
        return frameDisable;
    }

    public void setFrameDisable(boolean frameDisable) {
        this.frameDisable = frameDisable;
    }
}
